import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.github.davidmoten.guavamini.Lists;
import com.kennycason.kumo.CollisionMode;
import com.kennycason.kumo.WordCloud;
import com.kennycason.kumo.WordFrequency;
import com.kennycason.kumo.bg.CircleBackground;
import com.kennycason.kumo.bg.RectangleBackground;
import com.kennycason.kumo.font.FontWeight;
import com.kennycason.kumo.font.KumoFont;
import com.kennycason.kumo.font.scale.LinearFontScalar;
import com.kennycason.kumo.font.scale.SqrtFontScalar;
import com.kennycason.kumo.image.AngleGenerator;
import com.kennycason.kumo.nlp.FrequencyAnalyzer;
import com.kennycason.kumo.nlp.tokenizers.ChineseWordTokenizer;
import com.kennycason.kumo.palette.ColorPalette;

public class GenerateWordCloud extends MouseAdapter{
	public WordCloud wordCloud;
	
	public GenerateWordCloud (String[] args) throws IOException {
    	String [] words = args;
        List<String> s = Lists.newArrayList(words);
        Color[] colors = new Color[10];
        for (int i = 0; i < colors.length; i++) {
            colors[i] = new Color(
                    (new Double(Math.random() * 128)).intValue() + 128,
                    (new Double(Math.random() * 128)).intValue() + 128,
                    (new Double(Math.random() * 128)).intValue() + 128);
        }
       wordCloud  = buildWordCouldByWordFrequencies(200, 200, 4, 20, 10, buildWordFrequencies(s), new Color(-1), colors);
    }
	public void setWordCloud2File() {
		
		wordCloud.writeToFile("outputPic.png");
	}
	public void setWordCloud2NewWindow() {
		JFrame frame = new JFrame();
		frame.setSize(400, 400);
		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		
		JButton btn = new JButton("保存词云图片到本地");
		btn.addMouseListener(this);
		JPanel imgPanel = new ImagePanel();
		imgPanel.add(btn);
		frame.add(imgPanel);
		frame.setVisible(true);
	}
	public void mouseClicked(MouseEvent event) {
		setWordCloud2File();
	}
    private WordCloud buildWordCouldByWordFrequencies(int width_img, int height_img, int angle_word, int maxFont,
        int minFont, List<WordFrequency> wordFrequencies, Color backgroudColor, Color... wordColors) {
    	Dimension dimension = new Dimension(width_img, height_img);
    	WordCloud wordCloud = new WordCloud(dimension, CollisionMode.RECTANGLE);
    	wordCloud.setPadding(0);
    	wordCloud.setBackground(new RectangleBackground(dimension));
    	wordCloud.setKumoFont(new KumoFont("default", FontWeight.PLAIN));
    	wordCloud.setColorPalette(new ColorPalette(wordColors));
    	wordCloud.setFontScalar(new LinearFontScalar(minFont, maxFont));
		wordCloud.setBackgroundColor(backgroudColor);
		wordCloud.setAngleGenerator(new AngleGenerator(angle_word));
		wordCloud.build(wordFrequencies);
		return wordCloud;
    }
    private List<WordFrequency> buildWordFrequencies(List<String> words) {
        FrequencyAnalyzer frequencyAnalyzer = new FrequencyAnalyzer();
        frequencyAnalyzer.setWordFrequenciesToReturn(600);
        frequencyAnalyzer.setMinWordLength(2);
        frequencyAnalyzer.setWordTokenizer(new ChineseWordTokenizer());
        List<WordFrequency> wordFrequencies = frequencyAnalyzer.load(words);
        return wordFrequencies;
    }
    private class ImagePanel extends JPanel{
    	public void paintComponent(Graphics g) {
    		g.drawImage(wordCloud.getBufferedImage(),100, 50,null);
    	}
    }

}





